<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\goods;

use app\adminapi\controller\Base;
use app\service\goods\SpecService;
use app\adminapi\validate\goods\SpecValidate;

class Spec extends Base
{
    /**
     * Spec list
     */
    public function list()
    {
        $where = $this->request->params([
            ['name', ''],
            ['status', ''],
        ]);

        $service = new SpecService();
        $list    = $service->getSpecList($where[1], $this->limit);

        $this->success('', [
            'list'  => $list->items(),
            'total' => $list->total(),
        ]);
    }

    /**
     * Spec details
     */
    public function detail()
    {
        $id = inputs('id/d', 0);

        $service = new SpecService();
        $spec    = $service->getSpecDetail($id);

        $this->success('', $spec);
    }

    /**
     * Add spec
     */
    public function add()
    {
        $post = [
            'name'       => inputs('name/s', ''),
            'rule_value' => inputs('rule_value/a', []),
            'status'     => inputs('status/d', 1),
            'sort'       => inputs('sort/d', 0),
        ];

        $validate = new SpecValidate();
        $validate->scene('add')->failException(true)->check($post);

        $service = new SpecService();
        $result  = $service->saveSpec($post);

        $this->success('', $result);
    }

    /**
     * Edit spec
     */
    public function edit()
    {
        $post = [
            'id'         => inputs('id/d', 0),
            'name'       => inputs('name/s', ''),
            'rule_value' => inputs('rule_value/a', []),
            'status'     => inputs('status/d', 1),
            'sort'       => inputs('sort/d', 0),
        ];

        $validate = new SpecValidate();
        $validate->scene('edit')->failException(true)->check($post);

        $service = new SpecService();
        $result  = $service->saveSpec($post);

        $this->success('', $result);
    }

    /**
     * Delete spec
     */
    public function del()
    {
        $id = inputs('id/d', 0);

        $validate = new SpecValidate();
        $validate->scene('del')->failException(true)->check(['id' => $id]);

        $service = new SpecService();
        $service->deleteSpec($id);

        $this->success();
    }

    /**
     * Update spec status
     */
    public function status()
    {
        $id = inputs('id/d', 0);

        $validate = new SpecValidate();
        $validate->scene('status')->failException(true)->check(['id' => $id]);

        $service = new SpecService();
        $spec    = $service->getSpecDetail($id);
        $res     = $service->updateSpecStatus($id, $spec->status ? 0 : 1);

        $this->success();
    }

    /**
     * Get available spec list (no pagination)
     */
    public function getSpecOptions()
    {
        $service = new SpecService();
        $list    = $service->getAvailableSpecs();

        $this->success('', $list);
    }
}